/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.Graphics;

import javax.swing.JPanel;

import soccerscope.model.ClientScene;
import soccerscope.model.ColorDB;
import soccerscope.model.SceneSet;
import soccerscope.model.WorldModel;
import soccerscope.util.analyze.PassAnalyzer;
import soccerscope.view.FieldPane;

public class BallPossessionLayer extends Layer {

	public BallPossessionLayer(FieldPane fieldPane, boolean enable) {
		super(fieldPane, enable);
	}

	@Override
	public String getLayerName() {
		return "Ball Possession";
	}

	@Override
	public JPanel getConfigPanel() {
		return null;
	}

	@Override
	public void draw(Graphics g) {
		if (!this.enable)
			return;

		ClientScene clientScene = this.fieldPane.getScene();
		SceneSet set = WorldModel.getInstance().getSceneSet();

		int team = PassAnalyzer.getPossessionTeam(clientScene.time);
		if (team == PassAnalyzer.PLAY_OFF || team == PassAnalyzer.PLAY_ON)
			return;

		int startTime = clientScene.time;
		while (true) {
			startTime--;
			int t = PassAnalyzer.getPossessionTeam(startTime);
			if (t != team)
				break;
		}

		if (team == PassAnalyzer.LEFT_SIDE) {
			g.setColor(ColorDB.getColor("team_l_color"));
		} else if (team == PassAnalyzer.RIGHT_SIDE) {
			g.setColor(ColorDB.getColor("team_r_color"));
		}

		for (int time = startTime; time < clientScene.time; time++) {
			drawLine(g, set.getScene(time).ball.pos,
					set.getScene(time + 1).ball.pos);
		}
	}
}
